home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / CopyBitsSpeedTest / CopyBitsSpeedTest.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  5KB  |  188 lines

  1. #include <QDOffScreen.h>
  2.  
  3. /* Standard inits */
  4.  
  5.  
  6. #define    kLoopNumber    50
  7.  
  8.  
  9.  
  10. static void InitToolbox(void)
  11. {
  12.     InitGraf (&qd.thePort);
  13.     InitFonts ();
  14.     FlushEvents (everyEvent,0);
  15.     InitWindows ();
  16.     InitMenus ();
  17.     TEInit ();
  18.     InitDialogs (nil);
  19.     InitCursor ();
  20. } /*InitToolbox*/
  21.  
  22.  
  23. void main(void)
  24. {
  25.     WindowPtr    myWindow;
  26.     Rect    windowRectangle;
  27.     short    h,v;
  28.     GrafPtr    screenPort;
  29.     GDHandle    screenDevice;
  30.     Rect    pictRectangle, destRectange;
  31.     PicHandle    coolPicture;
  32.     CTabHandle    cTable;
  33.     GrafPtr    offscreenGWorld = nil;
  34.     
  35.     CGrafPtr    idioterIMetrowerks;
  36.     
  37. // offscreenGWorld is really a GWorldPtr rather than a GrafPtr, but I prefer to
  38. // refer to them by GrafPtrs, since the CopyBits calls get much cleaner.
  39.  
  40. /* Variables for loops, timing and display */
  41.     long    beforeTime, afterTime;
  42.     long    loop;
  43.     Str255    tempStr;
  44.  
  45.  
  46.     InitToolbox();
  47.  
  48. /* Load the picture*/
  49.     coolPicture = GetPicture(128);            /*PICT resource #128.*/
  50.     if (coolPicture == nil)
  51.     {
  52.         SysBeep(1);
  53.         ExitToShell();
  54.     }
  55. /* Get its frame */
  56.     pictRectangle = (**coolPicture).picFrame;
  57. /* Move it to 0,0 */
  58.     OffsetRect(&pictRectangle, -pictRectangle.left,  -pictRectangle.top);
  59.  
  60. /*Set up the window*/
  61.     windowRectangle = pictRectangle;
  62.     OffsetRect(&windowRectangle, 64, 64);
  63.     windowRectangle.bottom += 100;
  64.     myWindow = NewCWindow(0L, &windowRectangle, "\pCopyBitsSpeedTest", true, 0, (WindowPtr)-1L, false, 0);
  65.     SetPort(myWindow);
  66.  
  67.     DrawPicture(coolPicture, &pictRectangle);
  68.  
  69. /* Remember it so we can get back after visiting a GWorld */
  70.     GetGWorld((GWorldPtr *)&screenPort, &screenDevice);
  71.  
  72. /* Create a GWorld to draw the PICT in */
  73.     if ( noErr != NewGWorld((GWorldPtr *)&offscreenGWorld, 0, &pictRectangle, nil, nil, 0) )
  74.         ExitToShell();
  75. /*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
  76.     if ( LockPixels(((CGrafPtr)offscreenGWorld)->portPixMap) )
  77.         ;
  78.     SetGWorld((GWorldPtr)offscreenGWorld, nil);
  79.     
  80.     PaintRect(&pictRectangle);
  81.     DrawPicture(coolPicture, &pictRectangle);
  82.  
  83.     SetGWorld((GWorldPtr)screenPort, screenDevice);
  84.  
  85.     CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
  86.  
  87. // Aligned, optimal copy
  88.     beforeTime = TickCount();
  89.     for (loop = 0; loop < kLoopNumber; loop++)
  90.     {
  91.         EraseRect(&pictRectangle);
  92.         CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
  93.     }
  94.     afterTime = TickCount();
  95.     MoveTo(10, windowRectangle.bottom - 80 - 64);
  96.     NumToString(afterTime - beforeTime, tempStr);
  97.     DrawString("\pAligned copy: ");
  98.     DrawString(tempStr);
  99.     DrawString("\p ticks.");
  100.  
  101. // Wrong transfer mode
  102.     beforeTime = TickCount();
  103.     for (loop = 0; loop < kLoopNumber; loop++)
  104.     {
  105.         EraseRect(&pictRectangle);
  106.         CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcOr, nil);
  107.     }
  108.     afterTime = TickCount();
  109.     MoveTo(10, windowRectangle.bottom - 65 - 64);
  110.     NumToString(afterTime - beforeTime, tempStr);
  111.     DrawString("\psrcOr: ");
  112.     DrawString(tempStr);
  113.     DrawString("\p ticks.");
  114.  
  115. // Not longword aligned
  116.     destRectange = pictRectangle;
  117.     OffsetRect(&destRectange, 1, 0);
  118.  
  119.     beforeTime = TickCount();
  120.     for (loop = 0; loop < kLoopNumber; loop++)
  121.     {
  122.         EraseRect(&pictRectangle);
  123.         CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &destRectange, srcCopy, nil);
  124.     }
  125.     afterTime = TickCount();
  126.     MoveTo(10, windowRectangle.bottom - 50 - 64);
  127.     NumToString(afterTime - beforeTime, tempStr);
  128.     DrawString("\pUnaligned copy: ");
  129.     DrawString(tempStr);
  130.     DrawString("\p ticks.");
  131.  
  132. // Not same dimensions
  133.     destRectange = pictRectangle;
  134.     destRectange.right ++;
  135.  
  136.     beforeTime = TickCount();
  137.     for (loop = 0; loop < kLoopNumber; loop++)
  138.     {
  139.         EraseRect(&pictRectangle);
  140.         CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &destRectange, srcCopy, nil);
  141.     }
  142.     afterTime = TickCount();
  143.     MoveTo(10, windowRectangle.bottom - 35 - 64);
  144.     NumToString(afterTime - beforeTime, tempStr);
  145.     DrawString("\pMismatching dimensions: ");
  146.     DrawString(tempStr);
  147.     DrawString("\p ticks.");
  148.  
  149. // Color table mismatch
  150.  
  151. //myGWorldFlags := UpdateGWorld((GWorldPtr)offscreenGWorld,0,boundsRect,cTable,aGDevice,0);
  152.     UnlockPixels(((CGrafPtr)offscreenGWorld)->portPixMap);
  153.     DisposeGWorld((GWorldPtr)offscreenGWorld);
  154.     cTable = GetCTable(128);
  155. //    UpdateGWorld((GWorldPtr *)&offscreenGWorld,4,nil,cTable,nil,0);
  156.  
  157.  
  158. /* Create a GWorld to draw the PICT in */
  159.     if ( noErr != NewGWorld((GWorldPtr *)&offscreenGWorld, 8, &pictRectangle, cTable, nil, 0) )
  160.         ExitToShell();
  161. /*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
  162.     if ( LockPixels(((CGrafPtr)offscreenGWorld)->portPixMap) )
  163.         ;
  164.     SetGWorld((GWorldPtr)offscreenGWorld, nil);
  165.     
  166.     PaintRect(&pictRectangle);
  167.     DrawPicture(coolPicture, &pictRectangle);
  168.     
  169.     SetGWorld((GWorldPtr)screenPort, screenDevice);
  170.     
  171.     
  172.     beforeTime = TickCount();
  173.     for (loop = 0; loop < kLoopNumber; loop++)
  174.     {
  175.         EraseRect(&pictRectangle);
  176.         CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
  177.     }
  178.     afterTime = TickCount();
  179.     MoveTo(10, windowRectangle.bottom - 20 - 64);
  180.     NumToString(afterTime - beforeTime, tempStr);
  181.     DrawString("\pCLUT mismatch: ");
  182.     DrawString(tempStr);
  183.     DrawString("\p ticks.");
  184.  
  185.  
  186.     while (!Button());
  187. } /*main*/
  188.